package com.wmh.baseservice.common.shiro.jwt;

import com.alibaba.fastjson2.JSONObject;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import org.apache.shiro.authc.AuthenticationToken;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * JWT工具类
 */
@Component
public class JwtTokenUtil {

    /**
     * 密钥
     */
    private static final String secret = "h8$A5zB5wmh@8d3Nf";

    /**
     * 从数据声明生成令牌
     * @param claims 数据声明
     * @return 令牌
     */
    private static String generateToken(Map<String, Object> claims, long time) {
        Date expirationDate = new Date(System.currentTimeMillis() + time * 1000);
        return Jwts.builder().setClaims(claims).setExpiration(expirationDate).signWith(SignatureAlgorithm.HS512, secret).compact();
    }

    /**
     * 从令牌中获取数据声明
     * @param token 令牌
     * @return 数据声明
     */
    private static Claims getClaimsFromToken(String token) throws ExpiredJwtException {
        return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
    }


    /**
     * 从令牌中获取用户名
     * @param token 令牌
     * @return 用户名
     */
    public static Claims getUsernameFromToken(String token)  throws ExpiredJwtException{
        return getClaimsFromToken(token);
    }

    /**
     * 判断令牌是否过期
     * @param token 令牌
     * @return 是否过期
     */
    public static Boolean isTokenExpired(String token) {
        try {
            Claims claims = getClaimsFromToken(token);
            claims.getExpiration();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 刷新令牌
     * @param token 原令牌
     * @return 新令牌
     */
    public static String refreshToken(String token, long time) {
        String refreshedToken;
        try {
            Claims claims = getClaimsFromToken(token);
            claims.put("created", new Date());
            refreshedToken = generateToken(claims, time);
        } catch (Exception e) {
            refreshedToken = null;
        }
        return refreshedToken;
    }

    /**
     * 生成令牌
     * @return 令牌
     */
    public static String generateToken(String userName,long time) {
        Map<String, Object> claims = new HashMap<>(2);
        claims.put("account", userName);
        claims.put("created", new Date());
        return generateToken(claims, time);
    }

    /**
     * 验证令牌
     * @param token       令牌
     * @param userName 用户名
     * @return 是否有效
     */
    public static Boolean validateToken(String token,String userName) {
        String username = getUsernameFromToken(token).get("account").toString();
        return (username.equals(userName) && !isTokenExpired(token));
    }


    public static void main(String[] args) throws InterruptedException {
        Boolean tokenExpired = isTokenExpired("eyJhbGciOiJIUzUxMiJ9.eyJleHAiOjE2MzY2MDM0MzAsImFjY291bnQiOiJhZG1pbiIsImNyZWF0ZWQiOjE2MzY2MDMyNzA5NTF9.y40t_c43tqQ3jYYk1kBG8TvLU8T8GUK8ETuPY5histu8v-vorrGphXhK75zklynO5wEEYvtyg85G1rdcs1y6NA");
        System.out.println(tokenExpired);
    }




}
